package io.vincentwinner.systeminfoapi.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.vincentwinner.systeminfoapi.dao.ICentralProcessUnitDao;
import io.vincentwinner.systeminfoapi.dao.IDiskDao;
import io.vincentwinner.systeminfoapi.dao.IMemoryDao;
import io.vincentwinner.systeminfoapi.dao.IOperatingSystemDao;
import io.vincentwinner.systeminfoapi.dao.impl.CommonHardwareDaoImpl;
import io.vincentwinner.systeminfoapi.service.IStaticInfoService;
import io.vincentwinner.toolset.os.Disk;
import io.vincentwinner.toolset.os.Memory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @see IStaticInfoService
 */
@Service("staticInfoService")
public class StaticInfoServiceImpl implements IStaticInfoService {

    @Resource(name = "commonHardwareDao")
    private CommonHardwareDaoImpl common;

    @Resource(name = "osDao")
    private IOperatingSystemDao os;

    @Resource(name = "cpuDao")
    private ICentralProcessUnitDao cpu;

    @Resource(name = "memoryDao")
    private IMemoryDao memory;

    @Resource(name = "diskDao")
    private IDiskDao disk;

    /**
     * <pre>
     *     {
     *         serialNumber:  计算机序列号,
     *         hardwareUUID:  硬件唯一标识,
     *         manufacturer:  计算机制造商,
     *         computerModel: 计算机型号
     *     }
     * </pre>
     * @return 基本硬件信息
     */
    @Override
    public JSONObject getCommonHardwareInfo() {
        JSONObject jsonObject = new JSONObject(4);
        jsonObject.put("serialNumber",common.getSerialNumber());
        jsonObject.put("hardwareUUID",common.getHardwareUUID());
        jsonObject.put("manufacturer",common.getManufacturer());
        jsonObject.put("computerModel",common.getComputerModel());
        return jsonObject;
    }

    /**
     * <pre>
     *     {
     *         osName:    操作系统名称,
     *         arch:      操作系统架构,
     *         osVersion: 操作系统版本,
     *         userName:  当前用户用户名,
     *         userHome:  当前用户家目录,
     *         lineSeparator: 行分隔符,
     *         pathSeparator: 路径分隔符,
     *         fileSeparator: 文件路径分隔符,
     *         bit: 操作系统位数
     *     }
     * </pre>
     * @return 操作系统静态信息
     */
    @Override
    public JSONObject getOperatingSystemStaticInfo() {
        JSONObject jsonObject = new JSONObject(9);
        jsonObject.put("osName",os.getOSName());
        jsonObject.put("arch",os.getArch());
        jsonObject.put("osVersion",os.getOSVersion());
        jsonObject.put("userName",os.getOSUserName());
        jsonObject.put("userHome",os.getUserHome());
        jsonObject.put("lineSeparator",os.getLineSeparator().equals("\r\n") ? "\\r\\n" : os.getLineSeparator().equals("\n") ? "\\n" : "\\r");
        jsonObject.put("pathSeparator",os.getPathSeparator());
        jsonObject.put("fileSeparator",os.getFileSeparator());
        jsonObject.put("bit",os.getBit());
        return jsonObject;
    }

    /**
     * <pre>
     *     {
     *         cpuVendor: 处理器制造商,
     *         cpuName:   处理器产品名,
     *         cpuFamily: 处理器产品族,
     *         logicalCoreNum:  逻辑核心数量,
     *         physicalCoreNum: 物理核心数量,
     *         cpuModel:  CPU 型号,
     *         cpuStepping:     CPU 步进,
     *         cpuVendorFreq:   CPU 出厂频率,
     *         cpuMaxFreq:      CPU 最大频率
     *     }
     * </pre>
     * @return cpu静态信息
     */
    @Override
    public JSONObject getCentralProcessUnitStaticInfo() {
        JSONObject jsonObject = new JSONObject(9);
        jsonObject.put("cpuVendor",cpu.getVendor());
        jsonObject.put("cpuName",cpu.getCPUName());
        jsonObject.put("cpuFamily",cpu.getFamily());
        jsonObject.put("logicalCoreNum",cpu.getLogicalCoreNum());
        jsonObject.put("physicalCoreNum",cpu.getPhysicalCoreNum());
        jsonObject.put("cpuModel",cpu.getModel());
        jsonObject.put("cpuStepping",cpu.getStepping());
        jsonObject.put("cpuVendorFreq",cpu.getVendorFreq());
        jsonObject.put("cpuMaxFreq",cpu.getMaxFreq());
        return jsonObject;
    }

    /**
     * <pre>
     *     [
     *         {
     *              bankLabel:    bank标签,
     *              capacity:     内存容量,
     *              clockSpeed:   时钟频率,
     *              manufacturer: 制造商,
     *              memoryType:   内存类型
     *         },
     *         {...},
     *         ...
     *     ]
     * </pre>
     * @return 内存静态信息
     */
    @Override
    public JSONArray getMemoryStaticInfo() {
        JSONArray jsonArray = new JSONArray();
        List<Memory.MemoryHardwareInfo> memoryHardwareInfos = memory.getMemoryHardwareInfos();
        for (Memory.MemoryHardwareInfo memoryHardwareInfo : memoryHardwareInfos) {
            JSONObject jsonObject = new JSONObject(5);
            jsonObject.put("bankLabel",memoryHardwareInfo.getBankLabel());
            jsonObject.put("capacity",memoryHardwareInfo.getCapacity());
            jsonObject.put("clockSpeed",memoryHardwareInfo.getClockSpeed());
            jsonObject.put("manufacturer",memoryHardwareInfo.getManufacturer());
            jsonObject.put("memoryType",memoryHardwareInfo.getMemoryType());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    /**
     * <pre>
     *     [
     *         {
     *              diskName: 磁盘名称,
     *              volume:   卷名,
     *              label:    标签,
     *              logicVolume: 逻辑卷,
     *              mountPoint:  挂载点,
     *              description: 描述,
     *              type:        磁盘类型,
     *              options:     磁盘参数,
     *              uuid:        磁盘唯一标识,
     *              totalSpace:  硬盘容量,
     *              freeSpace:   硬盘空闲空间,
     *              usedSpace:   已使用的磁盘空间,
     *              usage:       磁盘使用率
     *         },
     *         {...},
     *         ...
     *     ]
     * </pre>
     * @return 磁盘静态信息
     */
    @Override
    public JSONArray getDiskStaticInfo() {
        JSONArray jsonArray = new JSONArray();
        List<Disk.DiskHardwareInfo> diskHardwareInfos = disk.getDiskHardwareInfos();
        for (Disk.DiskHardwareInfo diskHardwareInfo : diskHardwareInfos) {
            JSONObject jsonObject = new JSONObject(13);
            jsonObject.put("diskName",diskHardwareInfo.getName());
            jsonObject.put("volume",diskHardwareInfo.getVolume());
            jsonObject.put("label",diskHardwareInfo.getLabel());
            jsonObject.put("logicVolume",diskHardwareInfo.getLogicVolume());
            jsonObject.put("mountPoint",diskHardwareInfo.getMountPoint());
            jsonObject.put("description",diskHardwareInfo.getDescription());
            jsonObject.put("type",diskHardwareInfo.getType());
            jsonObject.put("options",diskHardwareInfo.getOptions());
            jsonObject.put("uuid",diskHardwareInfo.getUuid());
            jsonObject.put("totalSpace",diskHardwareInfo.getTotalSpace());
            jsonObject.put("freeSpace",diskHardwareInfo.getFreeSpace());
            jsonObject.put("usedSpace",diskHardwareInfo.getUsedSpace());
            jsonObject.put("usage",diskHardwareInfo.getUsage());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

}
